package com.openapi.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * * @Author: xw
 * * @Company: exsun
 * * @Date: 2021/6/10 17:07
 * * @Description:
 **/
public class RestTemplateUtil {
    private static RestTemplate restTemplate = restTemplate();

    /**
     * 请求超时设置
     */
    public static RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(15000);
        factory.setReadTimeout(15000);
        return new RestTemplate(factory);
    }

    /**
     * 含参post请求(json格式传递参数)
     *
     * @param url
     * @param map
     * @return ResponseEntity
     */
    public static ResponseEntity<Map> httpPostRequest(String url, Map<String, Object> map) throws RestClientException {
        HttpHeaders headers = new HttpHeaders();
        //定义请求参数类型，这里用json所以是MediaType.APPLICATION_JSON
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, Object>> request = new HttpEntity(map, headers);
        ResponseEntity<Map> entity = restTemplate.postForEntity(url, request, Map.class);
        return entity;
    }

    /**
     * post请求(json格式传递参数)
     *
     * @param url
     * @return
     */
    public static JSONObject httpPostForObject(String url, Map<String, Object> map) throws RestClientException {
        HttpHeaders headers = new HttpHeaders();
        //定义请求参数类型，这里用json所以是MediaType.APPLICATION_JSON
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, Object>> request = new HttpEntity(map, headers);
        JSONObject jsonObject = restTemplate.postForObject(url, request, JSONObject.class);
        return jsonObject;
    }

    /**
     * 含参post请求(form-data格式传递参数)
     *
     * @param url
     * @param map
     * @return ResponseEntity
     */
    public static ResponseEntity<Map> httpPostRequestByFormData(String url, MultiValueMap<String, Object> map) throws RestClientException {
        HttpHeaders headers = new HttpHeaders();
        //定义请求参数类型，这里用json所以是MediaType.APPLICATION_FORM_URLENCODED
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity(map, headers);
        ResponseEntity<Map> entity = restTemplate.postForEntity(url, request, Map.class);
        return entity;
    }

    /**
     * 含参post请求(form-data格式传递参数)
     *
     * @param url
     * @param map
     * @return JSONObject
     */
    public static JSONObject httpPostRequestByFormData2(String url, MultiValueMap<String, Object> map) throws RestClientException {
        HttpHeaders headers = new HttpHeaders();
        //定义请求参数类型，这里用json所以是MediaType.APPLICATION_FORM_URLENCODED
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity(map, headers);
        JSONObject jsonObject = restTemplate.postForObject(url, request, JSONObject.class);
        return jsonObject;
    }

    /**
     * get请求
     *
     * @param url
     * @return
     */
    public static HttpEntity<JSONObject> httpGetForEntity(String url) throws RestClientException {
        HttpEntity<JSONObject> entity = restTemplate.getForEntity(url, JSONObject.class);
        return entity;
    }

    /**
     * get请求
     *
     * @param url
     * @return
     */
    public static JSONArray httpGetForArray(String url) throws RestClientException {
        JSONArray jsonArray = restTemplate.getForObject(url, JSONArray.class);
        return jsonArray;
    }

    /**
     * get请求
     *
     * @param url
     * @return
     */
    public static JSONObject httpGetForObject(String url) throws RestClientException {
        JSONObject jsonObject = restTemplate.getForObject(url, JSONObject.class);
        return jsonObject;
    }

    /**
     * get请求(Map参数)
     *
     * @param url
     * @param map
     * @return
     */
    public static ResponseEntity getForEntity(String url, Map<String, Object> map) throws RestClientException {
        ResponseEntity entity = restTemplate.getForEntity(url, Map.class, map);
        return entity;
    }

}
